package com.xj.opcua.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期处理
 */
public class DateUtils {
    /**
     * 	时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    
    
    public final static String YEAR = "yyyy";
    public final static String MONTH = "yyyy-MM";
    
    /**
     * 时间格式(yyyyMMdd)
     */
    public final static String DATE_PATTERN_1 = "yyyyMMdd";
    
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
    public final static String DATE_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
    
    /**
     * 时间格式(yyyyMMddHHmmss)
     */
    public final static String DATE_TIME_PATTERN_ALLOW = "yyyyMMddHHmmss";
    
    /**
     * 时间格式(yyMMddHHmmss)
     */
    public final static String DATE_TIME_PATTERN_ALLOW_ID = "yyMMddHHmmss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }
    
    public static String format(String date, String pattern) throws ParseException {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            Date dateTime = df.parse(date);
            return df.format(dateTime);
        }
        return null;
    }

    /**
     * 计算距离现在多久，非精确
     *
     * @param date
     * @return
     */
    public static String getTimeBefore(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        } else if (hour > 0) {
            r += hour + "小时";
        } else if (min > 0) {
            r += min + "分";
        } else if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }

    /**
     * 计算距离现在多久，精确
     *
     * @param date
     * @return
     */
    public static String getTimeBeforeAccurate(Date date) {
        Date now = new Date();
        long l = now.getTime() - date.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String r = "";
        if (day > 0) {
            r += day + "天";
        }
        if (hour > 0) {
            r += hour + "小时";
        }
        if (min > 0) {
            r += min + "分";
        }
        if (s > 0) {
            r += s + "秒";
        }
        r += "前";
        return r;
    }
    
    /*

    	*获取当前时间之前或之后几分钟 minute

    */

    public static String getTimeByMinute(int minute) {

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MINUTE, minute);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }
    /**
     *	获取当前月份之前的12个月份的时间（yyyy-MM,包括当前月） 
     */
    public static List<String> getMonthList(){
    	int year = Integer.parseInt(DateUtils.format(new Date(),"yyyy"));
		 int month = Integer.parseInt(DateUtils.format(new Date(), "MM"));
		//int month = 10;
		int m = month;
		List<String> times = new ArrayList<String>();
		String time ="";
		for (int j = m; j > 0; j--) {
			if(m<=9) {
				time = year+"-0" + m;
			}else {
				time = year+"-" + m;
			}
			times.add(time);
			m--;
		}
		if(month<12) {
			year-=1;
			for (int j = 12; j > month; j--) {
				if(j<=9) {
					time = year+"-0" + j;
				}else {
					time = year+"-" + j;
				}
				times.add(time);
			}
		}
		return times;
    }
    
    /**
     * 	获取当前时间的前7天日期
     */
    public static List<String> pastDays() {
    	List<String> dayList = new ArrayList<String>();
    	for(int i =6;i>=0;i--) {
    		Calendar no = Calendar.getInstance();
			no.setTime(new Date());
			no.set(Calendar.DATE, no.get(Calendar.DATE) - i);
			Date time = no.getTime();
			String format = format(time,DATE_PATTERN);
			dayList.add(format);
    	}
		return dayList;
    }
    
    /**
            * 时间戳按指定格式转化为日期（String）
     * @param timestamp
     * @param pattern
     * @return
     */
    public static String convertTimestamp2Date(Long timestamp, String pattern) {
 
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
 
        return simpleDateFormat.format(new Date(timestamp));
    }
    
    /**
     * 时间相减,返回秒
     * @param date
     * @param pattern
     * @return
     */
    public static long getTimeByMinus(String date, String pattern) {
    	 
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 将两个String类型的时间转换为Date类型，从而计算差值、parse()方法的作用是将String类型的时间解析为Date类型
        Date d1;
        Date d2;
		try {
			d1 = df.parse(date);
			d2 = df.parse(pattern);
			long time = (d1.getTime() - d2.getTime() )/ 1000 ;
			return time;
		} catch (ParseException e) {
			return 0;
		}
    }

    public static String dayCount(){
        Date d = new Date();
        //1.日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //2.某天的日期
        Date da1 = null;
        try {
            da1 = sdf.parse("2020-1-1 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //3.输出间隔天数         getTime获取的是毫秒
        long dayCount = (d.getTime() - da1.getTime()) / (24 * 60 * 60 * 1000);
        String dayStr = "";
        if(dayCount<10){
            dayStr = "000"+dayCount;
        }else if (dayCount<100){
            dayStr = "00"+dayCount;
        }else if (dayCount<1000){
            dayStr = "0"+dayCount;
        }
        return dayStr;
    }
 
/*    public static void main(String[] args) {
 
        Long timestamp = System.currentTimeMillis();
 
        String pattern = "yyyy-MM-dd HH:mm:ss";
        System.out.println(timestamp);
        System.out.println(convertTimestamp2Date(timestamp, pattern));
 
    }*/

}
